﻿Imports System.ComponentModel
Imports System.Data.Linq

Public Class themCLB
    Dim hinh As New LayHinh
    Dim context As New QLDBDataContext
    Private Sub themCLB_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim svd = From san In context.SANVDs
                  Where Not (From clb In context.CAULACBOs Where clb.MaSan = san.MaSan).Any
                  Select san
        cboTenSan.DataSource = svd
        cboTenSan.DisplayMember = "TenSan"
        cboTenSan.ValueMember = "MaSan"
        Dim hlv = From h In context.HUANLUYENVIENs
                  Where Not (From clb In context.CAULACBOs Where clb.MaHLV = h.MaHLV).Any
                  Select h
        cboTenHLV.DataSource = hlv
        cboTenHLV.DisplayMember = "TenHLV"
        cboTenHLV.ValueMember = "MaHLV"
    End Sub

    Private Sub pic_Click(sender As Object, e As EventArgs) Handles pic.Click
        If OpenFile.ShowDialog = Windows.Forms.DialogResult.OK Then
            pic.BackgroundImage = hinh.ResizImg(OpenFile.FileName, pic.Width, pic.Height)
        End If

    End Sub

    Private Function laymax() As Integer
        Dim m = (From c In context.CAULACBOs Select c.MaCLB).Max()
        Return m
    End Function
    Private Sub tbOk_Click(sender As Object, e As EventArgs) Handles tbOk.Click
        Dim clb As New CAULACBO
        If OpenFile.FileName = "OpenFile" Then
            OpenFile.ShowDialog()
            clb.MaCLB = laymax() + 1
            clb.MaHLV = cboTenHLV.SelectedValue
            clb.TenCLB = txtTenDoi.Text
            clb.MaSan = cboTenSan.SelectedValue
            clb.LOGO = hinh.LuuHinh(OpenFile.FileName, "caulacbo\")
            context.CAULACBOs.InsertOnSubmit(clb)
            context.SubmitChanges()
            MessageBox.Show("Thêm Câu Lạc Bộ Thành Công.")
        Else
            clb.MaCLB = laymax() + 1
            clb.MaHLV = cboTenHLV.SelectedValue
            clb.TenCLB = txtTenDoi.Text
            clb.MaSan = cboTenSan.SelectedValue
            clb.LOGO = hinh.LuuHinh(OpenFile.FileName, "caulacbo\")
            context.CAULACBOs.InsertOnSubmit(clb)
            context.SubmitChanges()
            MessageBox.Show("Thêm Câu Lạc Bộ Thành Công.")
        End If
    End Sub
End Class